#!/bin/bash

set -e

if [[ -n "${CI}" ]]; then
    set -x
fi

function usage() {
    echo -n \
"Usage: $(basename "$0")

Build container images from templates.
"
}

if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
    if [ "${1:-}" = "--help" ]; then
        usage
    else
        # Strip the patch number off of GDAL_VERSION for tagging
        GDAL_MAJOR_VERSION=$(echo "${GDAL_VERSION}" | cut -d "." -f 1,2)

        sed -e "s/%%GDAL_VERSION%%/$GDAL_VERSION/" \
            -e "s/%%OPENJPEG_VERSION%%/$OPENJPEG_VERSION/" \
            -e "s/%%OPENJDK_VERSION%%/$OPENJDK_VERSION/" \
            -e "s/%%LIBPROJ_VERSION%%/$LIBPROJ_VERSION/" \
            -e "s/%%CORES%%/$CORES/" "Dockerfile-$VARIANT.template" > Dockerfile

        docker build -t "quay.io/azavea/openjdk-gdal:$GDAL_MAJOR_VERSION-jdk$OPENJDK_VERSION-$VARIANT" .

        ./scripts/test

        docker images
    fi
fi
